using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using Chatu.ControlLider.Web.Models.Domain;

namespace Chatu.ControlLider.Web.Models.Repositories
{
    public class SqlSelectionItemRepository : ISelectionItemRepository
    {
        public IList<SelectionItem> GetEducationLevels()
        {
            return GetSelectionItems(SelectionItemType.EducationLevel);
        }

        public IList<SelectionItem> GetEmployeeTypes()
        {
            return GetSelectionItems(SelectionItemType.EmployeeType);
        }

        private IList<SelectionItem> GetSelectionItems(SelectionItemType type)
        {
            IList<SelectionItem> items = new List<SelectionItem>();

            using (SqlConnection conn = new SqlConnection(DataSession.ConnectionString))
            {
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.CommandText = string.Format("Lider.usp_SelectionItem_Get{0}s", type);

                    conn.Open();

                    using (SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
                    {
                        if (reader != null)
                        {
                            while (reader.Read())
                            {
                                items.Add(new SelectionItem
                                              {
                                                  ID = reader.GetInt32(0),
                                                  Name = reader.GetString(1)
                                              }
                                    );
                            }
                        }
                    }
                }
            }

            return items;
        }
    }
}